﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TelerikStore.EbayService;

namespace TelerikStore
{
    public class ProductItem
    {
        #region Variables
        private string _id;
        private string _title;
        private string _description;
        private string _smallPicture;
        private string _largePicture;
        private string _primaryCategoryID;
        private Nullable<double> _price;
        private Condition _condition;
        private string _savingRate;
        private int _quantity;
        private string _timeLeft;
        #endregion

        #region Properties
        public string TimeLeft
        {
            get
            {
                return _timeLeft ?? string.Empty;
            }
            set
            {
                _timeLeft = value;
            }
        }

        public Condition Condition
        {
            get
            {
                return _condition ?? new Condition() { Name = string.Empty, Id = 0 };
            }
            set
            {
                _condition = value;
            }
        }

        public int Quantity
        {
            get
            {
                return _quantity;
            }
            set
            {
                _quantity = value;
            }
        }

        public string SavingRate
        {
            get
            {
                return _savingRate;
            }
            set
            {
                _savingRate = value;
            }
        }

        public Nullable<double> Price
        {
            get
            {
                return _price ?? 0;
            }
            set
            {
                _price = value;
            }
        }

        public string PrimaryCategoryID
        {
            get
            {
                return _primaryCategoryID ?? string.Empty;
            }
            set
            {
                _primaryCategoryID = value;
            }
        }

        public string LargePicture
        {
            get
            {
                return _largePicture ?? string.Empty;
            }
            set
            {
                _largePicture = value;
            }
        }

        public string SmallPicture
        {
            get
            {
                return _smallPicture ?? string.Empty;
            }
            set
            {
                _smallPicture = value;
            }
        }

        public string Description
        {
            get
            {
                return _description ?? string.Empty;
            }
            set
            {
                _description = value;
            }
        }

        public string Title
        {
            get
            {
                return _title ?? string.Empty;
            }
            set
            {
                _title = value;
            }
        }

        public string Id
        {
            get
            {
                return _id ?? string.Empty;
            }
            set
            {
                _id = value;
            }
        }
        #endregion

        #region Methods

        private string CalculateSavings(string savingRate, double convertedCurrentPrice)
        {
            try
            {
                double fullPrice = Math.Round((convertedCurrentPrice / (100 - double.Parse(savingRate))) * 100, 2);

                double discount = Math.Round((fullPrice * double.Parse(savingRate)) / 100, 2);

                return discount.ToString();
            }
            catch
            {

                return string.Empty;
            }

        }
        #endregion

        #region Constructors
        public ProductItem()
        {
        }

        public ProductItem(Item item)
        {
            _id = item.Id;
            _title = item.Title;
            _description = item.Description;
            _primaryCategoryID = item.PrimaryCategoryId;
            _price = item.CurrentPrice;
            _smallPicture = item.GalleryUrl;
            _largePicture = item.GalleryUrl;
            _quantity = 0;
            _condition = item.Condition;
            _timeLeft = item.TimeLeft;
        }

        public ProductItem(Deal deal)
        {
            _id = deal.Id;
            _title = deal.Title;
            _description = deal.Description;
            _primaryCategoryID = deal.PrimaryCategoryId;
            _price = deal.ConvertedCurrentPrice;
            _savingRate = CalculateSavings(deal.SavingsRate.Replace("%",""), (double)deal.ConvertedCurrentPrice);
            _smallPicture = deal.Picture175Url;
            _largePicture = deal.PictureUrl;
            _quantity = 0;
        }
        #endregion


    }
}